package com.collect.tanzania.zhiHelper;

import android.text.TextUtils;

import com.collect.tanzania.zhiBean.zhiReceive.ZHIRCheckInOutEntity;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRGroupUserEntity;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRLoginEntity;
import com.collect.tanzania.zhiGlobal.zhiAppSettings.ZHIProjectConstants;
import com.collect.tanzania.zhiUtils.SPUtils;
import com.collect.tanzania.zhiUtils.StringUtil;

import java.util.ArrayList;

public class ZHIMainProcessHelper {
    /**
     * token
     */
    private String token;
    /**
     * userId
     */
    private String userId;
    /**
     * loginId
     */
    private String loginId;
    /**
     * 姓名
     */
    private String fullName;
    /**
     * 密码
     */
    private String password;
    /**
     * 是否添加过催记
     */
    private boolean hadNewCaseRecord;
    /**
     * 等级
     */
    private String level;
    /**
     * 用户信息
     */
    private ZHIRLoginEntity mZHIRLoginEntity;
    /**
     * 打卡信息
     */
    private ZHIRCheckInOutEntity mZHIRCheckInOutEntity;
    /**
     * 催收员列表
     */
    private ArrayList<ZHIRGroupUserEntity> mGroupUserEntityArrayList;
    /**
     * 最近一次获取奖金接口的时间
     */
    public static long mLastRequestBunsApiTime = 0L;

    public static ZHIMainProcessHelper getInstance() {
        return instance;
    }

    private static final ZHIMainProcessHelper instance = new ZHIMainProcessHelper();

    public ZHIMainProcessHelper() {
        setUserId(SPUtils.getInstance().getString(ZHIProjectConstants.USER_ID));
        setFullName(SPUtils.getInstance().getString(ZHIProjectConstants.USER_NAME));
        setPassword(SPUtils.getInstance().getString(ZHIProjectConstants.USER_PASSWORD));
        setToken(SPUtils.getInstance().getString(ZHIProjectConstants.USER_TOKEN));
    }

    public String getUserId() {
        String gUserId = StringUtil.safeString(userId);
        if (TextUtils.isEmpty(gUserId)) {
            setUserId(SPUtils.getInstance().getString(ZHIProjectConstants.USER_ID));
            setFullName(SPUtils.getInstance().getString(ZHIProjectConstants.USER_NAME));
            setPassword(SPUtils.getInstance().getString(ZHIProjectConstants.USER_PASSWORD));
            setToken(SPUtils.getInstance().getString(ZHIProjectConstants.USER_TOKEN));
        }
        return this.userId;
    }

    public void setUserId(String userId) {
        this.userId = StringUtil.safeString(userId);
        SPUtils.getInstance().put(ZHIProjectConstants.USER_ID, this.userId, true);
    }

    public String getPassword() {
        return StringUtil.safeString(password);
    }

    public void setPassword(String password) {
        this.password = StringUtil.safeString(password);
        SPUtils.getInstance().put(ZHIProjectConstants.USER_PASSWORD, this.password, true);
    }

    public String getFullName() {
        String gFullName = StringUtil.safeString(fullName);
        if (TextUtils.isEmpty(gFullName)) {
            setUserId(SPUtils.getInstance().getString(ZHIProjectConstants.USER_ID));
            setFullName(SPUtils.getInstance().getString(ZHIProjectConstants.USER_NAME));
            setPassword(SPUtils.getInstance().getString(ZHIProjectConstants.USER_PASSWORD));
            setToken(SPUtils.getInstance().getString(ZHIProjectConstants.USER_TOKEN));
        }
        return this.fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = StringUtil.safeString(fullName);
        SPUtils.getInstance().put(ZHIProjectConstants.USER_NAME, this.fullName, true);
    }

    public String getLevel() {
        if (TextUtils.isEmpty(level)) {
            level = "B1";
        }
        return StringUtil.safeString(level);
    }

    public boolean isLevelB3Plus() {
        return getLevel().contains("B3") || getLevel().contains("+");
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public boolean isHadNewCaseRecord() {
        return hadNewCaseRecord;
    }

    public void setHadNewCaseRecord(boolean hadNewCaseRecord) {
        this.hadNewCaseRecord = hadNewCaseRecord;
    }

    public String getToken() {
        String gToken = StringUtil.safeString(token);
        if (TextUtils.isEmpty(gToken)) {
            setUserId(SPUtils.getInstance().getString(ZHIProjectConstants.USER_ID));
            setFullName(SPUtils.getInstance().getString(ZHIProjectConstants.USER_NAME));
            setPassword(SPUtils.getInstance().getString(ZHIProjectConstants.USER_PASSWORD));
            setToken(SPUtils.getInstance().getString(ZHIProjectConstants.USER_TOKEN));
        }
        return this.token;
    }

    public void setToken(String token) {
        this.token = token;
        SPUtils.getInstance().put(ZHIProjectConstants.USER_TOKEN, this.token, true);
    }

    public String getLoginId() {
        return StringUtil.safeString(loginId);
    }

    public void setLoginId(String loginId) {
        this.loginId = loginId;
    }

    public ArrayList<ZHIRGroupUserEntity> getGroupUserEntityArrayList() {
        if (mGroupUserEntityArrayList == null) {
            mGroupUserEntityArrayList = new ArrayList<>();
        }
        return mGroupUserEntityArrayList;
    }

    public void setGroupUserEntityArrayList(ArrayList<ZHIRGroupUserEntity> groupUserEntityArrayList) {
        this.mGroupUserEntityArrayList = groupUserEntityArrayList;
    }

    public ZHIRLoginEntity getLoginEntity() {
        if (mZHIRLoginEntity == null) {
            mZHIRLoginEntity = new ZHIRLoginEntity();
        }
        return mZHIRLoginEntity;
    }

    public void setLoginEntity(ZHIRLoginEntity loginEntity) {
        this.mZHIRLoginEntity = loginEntity;
    }

    public ZHIRCheckInOutEntity getZHIRCheckInOutEntity() {
        return mZHIRCheckInOutEntity;
    }

    public void setZHIRCheckInOutEntity(ZHIRCheckInOutEntity zhirCheckInOutEntity) {
        this.mZHIRCheckInOutEntity = zhirCheckInOutEntity;
    }

    public void loginV67(ZHIRLoginEntity zhiRLoginEntity, String fullName, String password) {
        try {
            this.fullName = StringUtil.safeString(fullName);
            this.password = StringUtil.safeString(password);
            this.token = zhiRLoginEntity.getToken();
            this.loginId = zhiRLoginEntity.getLoginId();
            setFullName(this.fullName);
            setPassword(this.password);
            setToken(this.token);
            setUserId(zhiRLoginEntity.getUserId());
            setLevel(zhiRLoginEntity.getDefaultLeven());
            setLoginEntity(zhiRLoginEntity);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void logout() {
        setFullName("");
        setPassword("");
        setUserId("");
        setLevel("");
        setLoginEntity(null);
    }
}
